package com.vvlive.caibao.smartcity.fragment;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Parcelable;

import com.vvlive.caibao.smartcity.base.BaseFragment;
import com.vvlive.caibao.smartcity.base.CalendarViews;
import com.vvlive.caibao.smartcity.base.GetDeptInfo;
import com.vvlive.caibao.smartcity.base.MeetingsListInterface;
import com.vvlive.caibao.smartcity.bean.Dept;
import com.vvlive.caibao.smartcity.bean.MeetingRoom;
import com.vvlive.caibao.smartcity.bean.WeekCellUI;
import com.vvlive.caibao.smartcity.callback.WeekCallBack;
import com.vvlive.caibao.smartcity.view.WeekView;

import java.util.Calendar;
import java.util.List;

import smartcity.vvlive.caibao.com.smartc.R;

/**
 * Meetings' week view
 * Created by zhj on 2015/10/15.
 * 会议的日历周视图
 */
public class WeekViewFragment extends BaseFragment implements CalendarViews {

    private static final String DATA_KEY = "week";
    private static Calendar mCalendar;
    private static int mMonth;
    private WeekView mWeekView;
    private WeekCallBack mCallBack;
    private MeetingsListInterface mInterface;
    private GetDeptInfo mGetDeptInfo;
    private Dept mDept;
    private MeetingRoom mRoom;
    private MeetingRoom[] mRooms;

    public static WeekViewFragment getInstance(Parcelable data) {
        mCalendar = Calendar.getInstance();
        mMonth = mCalendar.get(Calendar.MONTH);
        WeekViewFragment instance = new WeekViewFragment();
        Bundle bundle = new Bundle();
        if (data != null) {
            bundle.putParcelable(DATA_KEY, data);
            instance.setArguments(bundle);
        }
        return instance;
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        if (activity instanceof MeetingsListInterface) {
            mInterface = (MeetingsListInterface) activity;
        }
    }

    @Override
    public int getContentRes() {
        return R.layout.fragment_week_view;
    }

    @Override
    public void initFragment() {
        mWeekView = findView(R.id.week_view);
        mGetDeptInfo = new GetDeptInfo();
        mCallBack = new WeekCallBack(getActivity(), mCalendar, mWeekView, mGetDeptInfo);
        mInterface.setOnMeetingsPreparedListener(mCallBack, mCalendar, 2);
        mWeekView.setOnCellClickListener(mCallBack);
    }

    @Override
    public String getHeaderText() {
        return mCalendar.get(Calendar.YEAR) + "年" +
                       (mCalendar.get(Calendar.MONTH) + 1) +
                       "月，第" + mCalendar.get(Calendar.WEEK_OF_MONTH) + "周";
    }

    @Override
    public void prev() {
        changeWeek(-1);
    }

    @Override
    public void next() {
        changeWeek(1);
    }

    @Override
    public Calendar getDate() {
        return mCalendar;
    }

    @Override
    public void onSelectRoom(Dept dept, MeetingRoom[] meetingRoom) {
        mDept = dept;
        mRoom = meetingRoom[0];
        mRooms = meetingRoom;
        refresh();
    }

    @Override
    public void onResult(int requestCode, int resultCode, Intent data) {
        mInterface.refreshMeetings(mCallBack);
    }

    @Override
    public void onRefresh(OnRefreshListener onRefreshListener) {
        mCallBack.setOnRefreshListener(onRefreshListener);
        mInterface.setOnMeetingsPreparedListener(mCallBack, mCalendar, 2);
    }

    /**
     * @param count
     */
    private void changeWeek(int count) {

        mCalendar.set(Calendar.WEEK_OF_MONTH, mCalendar.get(Calendar.WEEK_OF_MONTH) + count);
        /*
         * If the month changes, request new meetings from server,
         * filter by week through these meetings existed already otherwise.
         */
        if (mMonth != mCalendar.get(Calendar.MONTH)) {
            mMonth = mCalendar.get(Calendar.MONTH);
            mInterface.setOnMeetingsPreparedListener(mCallBack, mCalendar, 2);
        } else {
            mCallBack.setCalendar(mCalendar);
            refresh();
        }
    }

    /**
     * 刷新
     */
    private void refresh() {

        if (mRooms != null && mRooms.length > 0) {
            mWeekView.resetRows(mCallBack.getWeekRowsByRooms(mRooms));
            List<WeekCellUI> uis = mCallBack.getWeekCellUIListByMeetingRooms(mRooms);
            mCallBack.addWeekCellUIs(uis);
        } else {
            toast("程序正在初始化，请稍后");
        }

    }

}
